﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WebTraderLiteCL;

namespace WebTraderLite.Pages
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session.Clear();
                createPortfolioTable();                
                wtlDataSource.SelectParameters.Add("userName", DbType.String, HttpContext.Current.User.Identity.Name);
                autoBuySqlDataSource.SelectParameters.Add("userName", DbType.String, HttpContext.Current.User.Identity.Name);                
            }
            
        }

        private void createPortfolioTable()
        {
            
            // Create columns
            DataTable dt = new DataTable();            
            dt.Columns.Add("Stock", typeof(string));
            dt.Columns.Add("Buy Price",typeof(string));
            dt.Columns.Add("Shares", typeof(float));
            dt.Columns.Add("Current Price", typeof(string));
            //dt.Columns.Add("Change", typeof(string));
            dt.Columns.Add("Market Value", typeof(string));
            dt.Columns.Add("Profit-Loss", typeof(string));
            dt.Columns.Add("% Change", typeof(string));
            dt.Columns.Add("Sell", typeof(HyperLink));
            HyperLink sellPage = new HyperLink();
            sellPage.Text = "SELL";
            
            sellPage.Visible = true;
            sellPage.NavigateUrl = "~/Pages/SellStock.aspx?"; //need to create the sell page and change this url

            
            
            //example stock, replace with a loop through of all stocks owned by user from database calls
            DBTransactions trans = new DBTransactions();
            User usr = new User( HttpContext.Current.User.Identity.Name);

            List<Stock> allstoks=trans.getUserStocks(usr);
            for (int i = 0; i < allstoks.Count; i++)
            {
                Stock stk = new Stock(allstoks.ElementAt(i).Symbol);
                float exampleBuyPrice = allstoks.ElementAt(i).Price;
                float exampleSharesOwned = allstoks.ElementAt(i).SharesOutstanding;
                float lastPrice = stk.Price;
                DataRow row = dt.NewRow();
                // Add a row to the datatable
                row["Stock"] = stk.Symbol;
                row["Buy Price"] = string.Format("{0:C}", exampleBuyPrice);
                row["Shares"] = exampleSharesOwned;
                row["Current Price"] = string.Format("{0:C}", lastPrice);
                //row["Change"]        = string.Format("{0:C}", 
                row["Market Value"] = string.Format("{0:C}", exampleSharesOwned * lastPrice);
                row["Profit-Loss"] = string.Format("{0:C}", lastPrice * exampleSharesOwned - exampleBuyPrice * exampleSharesOwned);
                row["% Change"] = string.Format("{0:0.00 %}", (lastPrice - exampleBuyPrice) / exampleBuyPrice);
                row["Sell"] = sellPage; //hyperlink to sell owned stock, not working yet!
                dt.Rows.Add(row);
                //row.AcceptChanges();
            }
            //Set datasource for gridview and bind the data
            gridViewPortfolio.DataSource = dt;
            gridViewPortfolio.DataBind();

            //Add gridview to tab panel (doesn't show up without adding it for some reason)
            tabPortfolio.Controls.Add(gridViewPortfolio);
            
        }

        protected void monStockODS_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["userID"] = HttpContext.Current.User.Identity.Name;
        }
        

        protected void monStockODS_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            //Get values for the input parameters to the deleteMonitoredStock() method
            e.InputParameters["Symbol"] = e.InputParameters["Symbol"].ToString();
            e.InputParameters["userName"] = HttpContext.Current.User.Identity.Name;            
        }

        protected void autoBuySqlDataSource_Deleting(object sender, SqlDataSourceCommandEventArgs e)
        {
            //autoBuySqlDataSource.DeleteParameters.Add("userName", DbType.String, HttpContext.Current.User.Identity.Name);
            
        }

        protected void gridViewAutoBuy_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            String userID = e.Keys["loginName"].ToString();
            String stockSym = e.Keys["stockSymbol"].ToString();

        }

       


    }
}